package com.glwan.fixedassetapp.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Response;

public class GsonUtil {
    private static final Gson gson = new Gson();

    public static <T> T fromJson(String json, Class<T> classOfT) {
        return gson.fromJson(json, getType(classOfT));
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        Log.e("json", json);
        return gson.fromJson(json, typeOfT);
    }

    public static <T> Type getType(Class<T> clazz) {
        return TypeToken.getParameterized(clazz).getType();
    }

    public static <T> T fromJson(Response response, Type responseDataListType) throws IOException {
        if (response == null || response.body() == null) {
            return null;
        } else {
            return fromJson(response.body().string(), responseDataListType);
        }

    }
}
